<div class="tab_item" style="display: block">
    <div class="toolbar clearfix">
        <div class="float_right">
        <#if editFlag ==1>
            <button class="btn btn_green add">新建</button>
            <button class="btn btn_green batch_add">批量新建</button>
        </#if>
        </div>
        <h3>箱体信息</h3>
    </div>
    <div class="grid" style="height:325px;">
        <table id="page_table_box_${pid}"></table>
    </div>
</div>


<script>

    (function () {
        var win = $('#${pid}');

        $('#page_table_box_${pid}').datagrid({
            fit: true,
            width: '100%',
            height: '100%',

            url: "${contextPath}/security/hdw/cabinet_box/page.htm?cabinetId=${cabinetId}",
            fitColumns: true,

            idField: 'id',
            singleSelect: true,
            selectOnCheck: false,
            checkOnSelect: false,
            autoRowHeight: false,
            rowStyler: gridRowStyler,
            columns: [
                [
                    {
                        title: '箱号',
                        align: 'center',
                        field: 'boxNum',
                        width: 30
                    },
                    {
                        title: '激活',
                        align: 'center',
                        field: 'activeFlag',
                        width: 20,
                        formatter: function (val, row) {
                            return showFlagName(val);
                        }
                    },
                    {
                        title: '电池ID',
                        align: 'center',
                        field: 'batteryId',
                        width: 40
                    },
                    {
                        title: '当前电量',
                        align: 'center',
                        field: 'batteryVolume',
                        width: 25,
                        formatter: function (val, row) {
                            if(val==null){
                                return '';
                            }
                            return val + "%";
                        }
                    },
                    {
                        title: '温度C°',
                        align: 'center',
                        field: 'temp',
                        width: 30
                    },
                    {
                        title: '状态',
                        align: 'center',
                        field: 'boxStatusName',
                        width: 30
                    },
                    {
                        title: '充电阶段',
                        align: 'center',
                        field: 'chargeStage',
                        width: 30,
                        formatter: function (val, row) {
                            switch (val) {
                                case 4:
                                    return '已充满'
                                case 5:
                                    return '过温保护'
                                case 6:
                                    return '欠压保护'
                                case 1:
                                    return '充电中'
                                default:
                                    return '空闲中';
                            }
                        }
                    },
                    {
                        title: '刷新时间',
                        align: 'center',
                        field: 'refreshTime',
                        width: 70
                    },
                    {
                        title: '操作',
                        align: 'center',
                        field: 'id',
                        width: 50,
                        formatter: function (val, row) {
                            var html = '<a href="javascript:view_box_record(\'CABINET_ID\', \'BOX_NUM\')">查看</a>|'
                                    + '<#if editFlag?? && (editFlag==1)><a href="javascript:edit_box_record(\'CABINET_ID\', \'BOX_NUM\')">修改</a>|<a href="javascript:clean(\'CABINET_ID\', \'BOX_NUM\')">清空</a>|<a href="javascript:delete_box_record(\'CABINET_ID\', \'BOX_NUM\')">删除</a>|</#if>'
                                    + '<a href="javascript:open_box(\'CABINET_ID\', \'BOX_NUM\')">开箱</a>';
                            return html.replace(/CABINET_ID/g, row.cabinetId).replace(/BOX_NUM/g, row.boxNum);
                        }
                    }
                ]
            ]
        });

        win.find('button.query').click(function () {
            var datagrid = $('#page_table_box_${pid}');
            var siteName = win.find('input[name=siteName]').val();
            var courierName = win.find('input[name=courierName]').val();
            datagrid.datagrid('options').queryParams = {
                siteName: siteName,
                courierName: courierName
            };
            datagrid.datagrid('load');
        });

        win.find('button.add').click(function () {
            App.dialog.show({
                css: 'width:300px;height:180px;overflow:visible;',
                title: '新建',
                href: "${contextPath}/security/hdw/cabinet_box/add.htm?cabinetId=${cabinetId}",
                event: {
                    onClose: function () {
                        list_box_reload();
                    }
                }
            });
        });

        win.find('button.batch_add').click(function () {
            App.dialog.show({
                css: 'width:300px;height:310px;overflow:visible;',
                title: '批量新建',
                href: "${contextPath}/security/hdw/cabinet_box/batch_add.htm?cabinetId=${cabinetId}",
                event: {
                    onClose: function () {
                        list_box_reload();
                    }
                }
            });
        });

        var ok = function () {
            return true;
        };

        win.data('ok', ok);
    })();

    function delete_box_record(cabinetId, boxNum) {
        $.messager.confirm('提示信息', '确认删除?', function (ok) {
            if (ok) {
                $.post('${contextPath}/security/hdw/cabinet_box/delete.htm',
                        {
                            cabinetId: cabinetId,
                            boxNum: boxNum
                        }, function (json) {
                            if (json.success) {
                                list_box_reload();
                                $.messager.alert('提示消息', '操作成功', 'info');
                            } else {
                                $.messager.alert('提示消息', json.message, 'info');
                            }
                        }, 'json');
            }
        });
    }

    function open_box(cabinetId, boxNum) {
        $.messager.confirm('提示信息', '确认开箱?', function (ok) {
            if (ok) {
                $.post('${contextPath}/security/hdw/cabinet_box/open_box.htm',
                        {
                            cabinetId: cabinetId,
                            boxNum: boxNum
                        }, function (json) {
                            $.messager.alert('提示消息', json.message, 'info');
                        }, 'json');
            }
        });
    }

    function edit_box_record(cabinetId, boxNum) {
        App.dialog.show({
            css: 'width:300px;height:180px;overflow:visible;',
            title: '修改',
            href: "${contextPath}/security/hdw/cabinet_box/edit.htm?cabinetId=" + cabinetId + "&boxNum=" + boxNum,
            event: {
                onClose: function () {
                    list_box_reload();
                }
            }
        });
    }
    function clean(cabinetId, boxNum) {
        $.messager.confirm('提示信息', '确认清空?', function (ok) {
            if (ok) {
                $.post('${contextPath}/security/hdw/cabinet_box/clean.htm',
                        {
                            cabinetId: cabinetId,
                            boxNum: boxNum
                        }, function (json) {
                            if (json.success) {
                                list_box_reload();
                                $.messager.alert('提示消息', '操作成功', 'info');
                            } else {
                                $.messager.alert('提示消息', json.message, 'info');
                            }
                        }, 'json');
            }
        });
    }

    function view_box_record(cabinetId, boxNum) {
        App.dialog.show({
            css: 'width:566px;height:290px;overflow:visible;',
            title: '查看',
            href: "${contextPath}/security/hdw/cabinet_box/view.htm?cabinetId=" + cabinetId + "&boxNum=" + boxNum,
            event: {
                onClose: function () {
                }
            }
        });
    }

    function list_box_reload() {
        var datagrid = $('#page_table_box_${pid}');
        datagrid.datagrid('reload');
    }

    var showFlagName = function (flag) {
        if (flag == 1) {
            return "是";
        }
        return "否";
    }

</script>